<?php
namespace app\spadmin\controller;
use app\common\controller\Backend;
class Account extends Backend{
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Account');
    }

    public function index(){
        $this->request->filter(['strip_tags', 'trim']);
        
        if($this->request->isAjax()){
            $data = $this->request->param();
            if (isset($data['keywords']) && $data['keywords']) {
                $map['account|name'] = ['like','%'.$data['keywords'].'%'];
            }
            if (isset($data['status']) && $data['status'] === '0') {
                $map['status'] = $data['status'];
            } else {
                $map['status'] = 1;
            }     
            $list =   $this->model->where($map)->order('id desc')->paginate($data['limit']);
            foreach ($list as &$li) {
                $li->toArray();
            }
            return $this->result($list);
        }
        return $this->view->fetch();
    }
    public function add(){
        if($this->request->isPost()){
            $nums = $this->request->post('nums',1);
            $data = [];
            for ($i=0; $i < $nums; $i++) { 
                $data[$i]['secret'] = random_int(100000,999999);
                $data[$i]['account'] = '';
            }
            $result ="";
            $accounts = $this->model->saveAll($data);
            foreach ($accounts as $key => $value) {
                $result .= $value['account']." ".$value['secret']."\r\n";
            }
            $this->success('',null,$result);
        }
    }
}